/*
 * @Author: your name
 * @Date: 2021-03-21 22:10:39
 * @LastEditTime: 2021-03-23 00:16:00
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue-glshaop-client\src\store\addCart.js
 */
import {
    addToCart,
    shopCartList,
    changeShopCartNum,
    isCheckedOne,
    isCheckedAll,
    deleteOne,
    deleteAll
} from '@/api'
const state = {
    shopCartList: []
}
const mutations = {
    RESOVE_SHOPCARTLIST(state, shopCartList) {
        state.shopCartList = shopCartList
    }
}
const actions = {
    async getAddToCart({
        commit
    }, {
        skuId,
        skuNum
    }) {
        const result = await addToCart(skuId, skuNum)
        if (result.code === 200) {
            return Promise.resolve()
        } else {
            return Promise.reject()
        }
    },
    //购物车
    async getShopCartLists({
        commit
    }) {
        const result = await shopCartList()
        if (result.code === 200) {
            commit('RESOVE_SHOPCARTLIST', result.data)
        }
    },
    //购物车商品数量修改
    async getChangeShopCartNum({
        commit
    }, {
        skuId,
        skuNum
    }) {
        const result = await changeShopCartNum(skuId, skuNum)
        if (result.code === 200) {
            return Promise.resolve()
        } else {
            return Promise.reject()
        }
    },
    //修改单个状态
    async getIsCheckedOne({
        commit
    }, {
        skuId,
        isChecked
    }) {
        const result = await isCheckedOne(skuId, isChecked)
        if (result.code === 200) {
            return Promise.resolve()
        } else {
            return Promise.reject()
        }
    },
    //修改全部状态
    async getIsCheckedAll({
        commit
    }, {
        isChecked,
        skuIds
    }) {
        const result = await isCheckedAll(isChecked, skuIds)
        if (result.code === 200) {
            return Promise.resolve()
        } else {
            return Promise.reject()
        }
    },
    //删除单个商品
    async getDeleteOne({
        commit
    }, skuId) {
        const result = await deleteOne(skuId)
        if (result.code === 200) {
            return Promise.resolve()
        } else {
            return Promise.reject()
        }
    },
    async getDeleteAll({
        commit
    }, skuIds) {
        const result = await deleteAll(skuIds)
        if (result.code === 200) {
            return Promise.resolve()
        } else {
            return Promise.reject()
        }
    }
}
const getters = {
    isCheckedNum(state) {
        return state.shopCartList.reduce((prev, item) => {
            prev += item.cartInfoList.reduce((prev1, item1) => {
                if (item1.isChecked) {
                    prev1 += item1.skuNum
                }
                return prev1
            }, 0)
            return prev
        }, 0)
    },
    isCheckedPrice(state) {
        return state.shopCartList.reduce((prev, item) => {
            return prev += item.cartInfoList.reduce((prev1, item1) => {
                if (item1.isChecked) {
                    prev1 += item1.skuNum * item1.skuPrice
                }
                return prev1
            }, 0)
        }, 0)
    }
}
export default {
    state,
    mutations,
    actions,
    getters
}